import {
  api
} from '@/api'
import router from '@/router'
import Layout from '@/components/layout'
const _import = require('../../router/_import_' + process.env.NODE_ENV)
const state = {
  promptData: '',
  isFirst: true,
  isAuth: false,
  isW7: false,
  isShowPrompt: false, // 是否显示到期提醒
  systemCopyInfo: '', // 获取微擎系统版本信息
  routes: [],
  notice_num: 0,
  userInfo:{}
}
const getters = {
  userInfo: state => {
    return state.userInfo
  },
  isAuth: state => {
    return state.isAuth
  },
  isW7: state => {
    return state.isW7
  },
  routes: state => {
    return state.routes
  }
}
const mutations = {
  changeUserInfo (state, item) {
    state.userInfo = item
  },
  // 修改到期提醒数据
  changePromptData (state, item) {
    let { status } = item
    if (status === 2) {
      state.isShowPrompt = true
    } else if (status === 1 && state.isFirst) {
      state.isFirst = false
      state.isShowPrompt = true
    }
    state.promptData = item
  },
  // 修改是否显示到期提醒
  changeIsShowPrompt (state, item) {
    state.isShowPrompt = item
  },
  changeRoutesItem (state, item) {
    console.log('changeItem ==>', item)
    let { key, val } = item
    state[key] = val
  },
  saveRoutes (state, item = []) {
    state.routes = item
    state.isAuth = true
  }
}
const actions = {
  isWeiVersion ({ commit }, obj) {
    let data = { key: 'isW7', val: obj }
    commit('changeRoutesItem', data)
  },
  getUserPromission ({
    commit
  }, obj) {
    // 正式请求接口获取路由
    api.base.getW7TmpV2().then(res => {
      if (res.code === 200) {
        let { w7tmp } = res.data
        commit('changeRoutesItem', { key: 'systemCopyInfo', val: w7tmp })
      }
    })
    api.base.getSaasAuth().then(res => {
      if (res.code === 200) {
        let { h5, app, wechat } = res.data
        let allRoutes = obj.routes
        if (h5 && app && wechat) {
        } else {
          allRoutes.map(item => {
            if (item.path === '/sys') {
              if (!wechat) {
                item.meta.subNavName[0].url.splice(1, 1)
                item.meta.subNavName[1].url.splice(0, 1)
              }
              if (!h5) {
                item.meta.subNavName[1].url.splice(!wechat ? 0 : 1, 1)
              }
              if (!app) {
                item.meta.subNavName[1].url.splice(!wechat ? !h5 ? 0 : 1 : 1, 1)
              }
            }
          })
        }
        commit('saveRoutes', allRoutes)
        routerGo(allRoutes, obj)
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}

function routerGo (routes, obj) {
  let getRouter = filterAsyncRouter(routes) // 过滤路由
  router.options.routes.push(...getRouter)
  router.addRoutes(getRouter) // 动态添加路由
  // localStorage.setItem('routes', JSON.stringify(getRouter))
  obj.next({
    ...obj.to,
    replace: true
  })
}

function filterAsyncRouter (asyncRouterMap) { // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.component) {
      if (route.component === 'Layout') { // Layout组件特殊处理
        route.component = Layout
      } else {
        route.component = _import(route.component)
      }
    }
    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
  return accessedRouters
}
